// 云函数入口文件
const cloud = require('wx-server-sdk')
const fs = require('fs'); //文件模块
const path = require('path'); //系统路径模块

cloud.init()

// 云函数入口函数
exports.main = async(event, context) => {
  const logger = cloud.logger()
  var file = null
  switch (event.action) {
    case 'getVideoData': //获取视频列表
      {
        //文件路径，__dirname为当前运行js文件的目录
        file = path.join(__dirname, 'video-data.json');
        var result = JSON.parse(fs.readFileSync(file, 'utf-8'));
        return result;
      }
      break;
    case 'getVideoInfo': //获取视频详情列表
      {
        var vidieoKey = event.videoKey
        //文件路径，__dirname为当前运行js文件的目录
        file = path.join(__dirname, 'data/videoFileName.json'.replace(/videoFileName/, vidieoKey));
        var result = JSON.parse(fs.readFileSync(file, 'utf-8'));
        var newResult = getFullPath(result, vidieoKey)
        logger.error({
          newResult: newResult
        })
        return newResult;
      }
      break;
    case 'getLottieData'://获取lottie动画文件
    {
      var lottieName = event.lottieName
      //文件路径，__dirname为当前运行js文件的目录
      file = path.join(__dirname, 'lottie/lottieFileName.json'.replace(/lottieFileName/, lottieName));
      var result = JSON.parse(fs.readFileSync(file, 'utf-8'));
      return result;
    }
      break  
    default:
      return "请传入action值。包括：getVideoData、getVideoInfo,getLottieData"
  }
}

// 获取全路径方法
function getFullPath(videoArray, videoKey) {
  var p_path = 'cloud://dev-gt6sj.6465-dev-gt6sj-1258401079/douyin/videoFileName/'.replace(/videoFileName/, videoKey)
  var p_path = 'cloud://beat-22g19.6265-beat-22g19-1258401079/douyin/videoFileName/'.replace(/videoFileName/, videoKey)
  videoArray.forEach(function(video) {
    video.full_path = p_path + video.new_name
  })
  return videoArray;
}